﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ViewOnCore.AddIn;
using ViewOnCore.HostContract;

namespace iPodDeviceAddIn
{
    [AddIn("iPodMediaDevice", Version = "0.5.0.0", Publisher = "ViewOn Media Services")]
    sealed public class AddIn
    {
        private iPodMediaDevice _iPod;
        private bool _isiPodAlreadyConnected = false;

        public AddIn()
        {
            _iPod = new iPodMediaDevice();

            Host.MediaService.HardwareDeviceConnected += new HardwareDeviceEventHandler(MediaService_HardwareDeviceConnected);
            Host.MediaService.HardwareDeviceDisconnected += new HardwareDeviceEventHandler(MediaService_HardwareDeviceDisconnected);

            if (_iPod.IsDeviceReady)
            {
                MediaService_HardwareDeviceConnected(_iPod.iPod.FileSystem.DriveLetter);
            }
        }

        void MediaService_HardwareDeviceDisconnected(string driveLetter)
        {
            if (_isiPodAlreadyConnected && !_iPod.IsDeviceReady)
            {
                _isiPodAlreadyConnected = false;
                _iPod.DriveLetter = driveLetter;
                Host.MediaService.RemoveMediaDevice(_iPod);
            }
        }

        void MediaService_HardwareDeviceConnected(string driveLetter)
        {
            if (!_isiPodAlreadyConnected && _iPod.IsDeviceReady)
            {
                _isiPodAlreadyConnected = true;
                Host.MediaService.AddMediaDevice(_iPod);
            }
        }
    }
}
